var sha1 = require('sha1');

exports.getTimestamp = function () {
    return Math.floor(Date.now() / 1000);
};

// generate signature for wechat via sha1
exports.getSignature = function (input) {
    var string1 = "";
    if (typeof input == "string" && input != "") {
        string1 = input;
    }
    else if (typeof input == "object") {
        console.log("Generating signature for object\n", input);
        var keys = Object.keys(input).sort();
        var lenn = keys.length;
        for (var i = 0; i < lenn; ++i) {
            var k = keys[i];
            string1 += k + "=" + input[k];
            if (i < lenn - 1)
                string1 += "&";
        }
    } else {
        logger.error("Please supply a valid string or object as input");
        return input;
    }
    return sha1(string1);
};

